using FytSoa.Common.Extensions;
using FytSoa.Domain.Hr;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Hr;

/// <summary>
/// 服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v8")]
public class HrOrganizationService : IApplicationService
{
    private readonly SugarRepository<HrOrganization> _thisRepository;
    public HrOrganizationService(SugarRepository<HrOrganization> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<HrOrganizationDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<HrOrganizationDto>>();
    }

    /// <summary>
    /// 查询所有
    /// </summary>
    /// <returns></returns>
    public async Task<List<HrOrganizationDto>> GetListAsync(WhereParam param)
    {
        var list = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Name.Contains(param.Key))
                    .OrderByDescending(m=>m.Id)
                    .ToListAsync();
        return list.Adapt<List<HrOrganizationDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<HrOrganizationDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<HrOrganizationDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task AddAsync(HrOrganizationDto model)
    {
        var isAny = await _thisRepository.IsAnyAsync(m => m.Number == model.Number);
        if (isAny)
        {
            throw new BusinessException("编号不能为空！");
        }
        
        var addId=await _thisRepository.InsertReturnSnowflakeIdAsync(model.Adapt<HrOrganization>());
        if (model.ParentId==0)
        {
            await _thisRepository.UpdateAsync(m => new HrOrganization()
            {
                ParentIdList = new List<string> {addId.ToString() }
            },m=>m.Id==addId);
        }
        else
        {
            var parentModel = await _thisRepository.GetFirstAsync(m => m.Id == model.ParentId);
            parentModel.ParentIdList.Add(addId.ToString());
            await _thisRepository.UpdateAsync(m => new HrOrganization()
            {
                ParentIdList = parentModel.ParentIdList
            },m=>m.Id==addId);
        }
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task ModifyAsync(HrOrganizationDto model)
    {
        var isAny = await _thisRepository.IsAnyAsync(m => m.Number == model.Number && m.Id!=model.Id);
        if (isAny)
        {
            throw new BusinessException("编号不能为空！");
        }
        if (model.ParentId==0)
        {
            model.ParentIdList = new List<string>() {model.Id.ToString() };
            await _thisRepository.UpdateAsync(model.Adapt<HrOrganization>());
        }
        else
        {
            var parentModel = await _thisRepository.GetFirstAsync(m => m.Id == model.ParentId);
            parentModel.ParentIdList.Add(model.Id.ToString());
            model.ParentIdList = parentModel.ParentIdList;
            await _thisRepository.UpdateAsync(model.Adapt<HrOrganization>());
        }
        
    }

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) => 
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));

}

